using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;

namespace Atechnology.glassCAD.Shapes.CutViewer
{
    enum FilHeader
    {
        SizeLine, 
        End
    }

    public class GladParser : IParser
    {
        List<string> lines;

        private CutModel[] models;

        public CutModel[] Models
        {
            get { return models; }
            set { models = value; }
        }

        #region IParser Members

        public CutModel Parse(string filePath)
        {
            
            Stream str = File.Open(filePath, FileMode.Open);
            CutModel result = this.Parse(str);
            str.Close();
            return result;
        }

        public CutModel Parse(System.IO.Stream stream)
        {
            CutModel model = new CutModel();
            string line = "";
            StreamReader reader = new StreamReader(stream);
            lines = new List<string>();
            //lines.Clear();                                    
            while (line != null)
            {
                line = reader.ReadLine();
                if (line != null && this.ReadHeader(line) == FilHeader.SizeLine)
                    lines.Add(line);
            }
            if (lines.Count == 0)
            {
                reader.Close();
                return null;
            }

            models = new CutModel[lines.Count];

            for (int i = 0; i < lines.Count; i++)
            {
                models[i] = new CutModel();
                models[i].Figures = this.ParseSize(i);
            }

            reader.Close();
            return models[0];
        }

        #endregion

        private FilHeader ReadHeader(string line)
        {
            string header = line.Substring(0, 2);
            switch (header)
            {
                case "00":                    
                    return FilHeader.End;
                default:
                    return FilHeader.SizeLine;
            }
        }

        private List<IGeometry> ParseSize(int index)
        {
            List<IGeometry> linesList = new List<IGeometry>();
            
            string xSize = lines[index].Substring(10, 4);
            string ySize = lines[index].Substring(14, 4);

            PointF[] points = new PointF[2];
            points[0] = new PointF(0, 0);
            points[1] = new PointF(float.Parse(xSize), 0);
            linesList.Add(new Line(points));

            points = new PointF[2];
            points[0] = new PointF(float.Parse(xSize), 0);
            points[1] = new PointF(float.Parse(xSize), float.Parse(ySize));
            linesList.Add(new Line(points));

            points = new PointF[2];
            points[0] = new PointF(float.Parse(xSize), float.Parse(ySize));
            points[1] = new PointF(0, float.Parse(ySize));
            linesList.Add(new Line(points));

            points = new PointF[2];
            points[1] = new PointF(0, float.Parse(ySize));
            points[1] = new PointF(0, 0);
            linesList.Add(new Line(points));
            return linesList;
        }
    }
}
